<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到院长信箱系统</h2>
          <el-form-item prop="userAccount">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.userAccount"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              :show-password="true"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login_btn"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
// 引入获取当前小时的方法
import { getTime } from '@/utils/time'
// 引入并使用用户小仓库
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
// 引入路由对象并使用
import { useRoute } from 'vue-router'
const $route = useRoute()
// 登录按钮的加载
let loading = ref(false)
// 获取路由器
const $router = useRouter()
// 收集账号密码
const loginForm = reactive({
  userAccount: 'yhnyhn123',
  password: '1658450511',
})
// 获取el-form组件
const loginForms = ref()
// 登录按钮的回调
const login = async () => {
  await loginForms.value.validate()
  // 登录按钮的加载效果
  loading.value = true
  // 通知仓库发登录请求
  try {
    await userStore.userLogin(loginForm)
    // 请求成功 -> 转到首页
    // 判断有无query参数，再进行跳转
    let redirect: any = $route.query.redirect
    $router.push(redirect ? redirect : '/home')
    ElNotification({
      type: 'success',
      message: '欢迎回来！',
      title: `Hello,${getTime()}好！`,
    })
    loading.value = false
  } catch (error: any) {
    // 关闭加载效果
    loading.value = false
    // 请求失败->弹出失败信息
    ElNotification({
      type: 'error',
      message: error.message,
    })
  }
}
// 定义规则
const rules = {
  userAccount: [
    {
      required: true,
      min: 5,
      max: 20,
      message: '用户名长度不合规范',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 20,
      message: '密码格式不符合',
      trigger: 'change',
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/background.jpg');
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 150px 90px 0 90px;
    background-image: url('@/assets/login_form.png');
    background-size: cover;

    h1 {
      position: absolute;
      top: 30px;
      left: 20px;
      font-size: 45px;
      color: white;
    }

    h2 {
      position: absolute;
      top: 100px;
      left: 20px;
      font-size: 24px;
      color: white;
    }
  }

  .login_btn {
    width: 100%;
    margin-bottom: 20px;
  }
}
</style>
